<?php
/**
 * File : content.class.php
 * Version : 0.0.1
 * Author : apple
 * Update : 2009/05/05 15:00
 */

class content
{
	var $db;
	var $table;
	var $pages;
	var $url;
	var $html;

	function __construct()
	{
		global $db, $tablepre;
		$this->db = &$db;
		$this->table = "{$tablepre}content";
		$this->table_attach = "{$tablepre}attach";
		$this->table_zt = "{$tablepre}special";	//专题
	}

	function content()
	{
		$this->__construct();
	}

	function get($contentid)
	{
		$contentid = intval($contentid);
		$data = $this->db->fetch_first("SELECT * FROM `$this->table` WHERE `contentid`='$contentid'");
		return $data;
	}

	function getlist($where, $offset = 0, $size = 5, $order = '`pubtime` DESC')
	{
		$order = " ORDER BY $order";
		$limit = " LIMIT $offset, $size";
		$sql = '';
		$sql = "SELECT `contentid`, `title`, `stitle`, `description`, `editor`, `islink`, `url`, `pubtime`, `updatetime`, `status` FROM `$this->table` WHERE `status`>0 $where $order $limit";
		$data = $this->db->fetch_all($sql);
		return $data;
	}

	function add($data)
	{
		global $timestamp, $administrator;
		$title = $data['title'];
		$stitle = $data['stitle'];
		$cateid = $data['subcateid'] ? $data['subcateid'] : $data['cateid'];
		$origin = $data['origin'];
		$author = $data['author'];
		$editorid = $administrator['id'];
		$editor = $administrator['editor'];
		$filepath = $data['filepath'];
		$filename = $data['filename'];
		$content = SafeConvert($data['content']);
		$description = $data['description'] ? $data['description'] : cutstr(strip_tags($content), 50);
		$islink = $data['islink'];
		$special = $data['special'];
		$status = $data['weighing'] && $data['weighing']>2 && $data['weighing']<99 ? $data['weighing'] : $data['status'];
		$showimg = $data['showimg'];
		$this->db->query("INSERT INTO `$this->table`(`cateid`, `title`, `stitle`, `description`, `origin`, `author`, `editorid`, `editor`, `content`, `showimg`, `islink`, `pubtime`, `updatetime`, `status`, `special`)
		 VALUES('$cateid', '$title', '$stitle', '$description', '$origin', '$author', '$editorid', '$editor', '$content', '$showimg', '$islink', '$timestamp', '$timestamp', '$status', '$special')");
		$contentid = $this->db->insert_id();

		//更新URL
		if($special == 1 && $data['filepath'] && $data['filename']){
			$url = 'zt/'.$data['filepath'].'/'.$data['filename'].'.shtml';
		}else{
			$url = $data['islink'] ? $data['url'] : ('a/'.date('y').'/'.date('m').'/'.$contentid.'.shtml');
		}
		$this->db->query("UPDATE `$this->table` SET `url`='$url' WHERE `contentid`='$contentid'");

		//特殊主题
		if($special == 1 && !$islink) //专题
		{
			$headextend = $data['headextend'];
			$banner = $data['banner'];
			$template = $data['template'];
			$filepath = $data['filepath'];
			$filename = $data['filename'];
			$this->db->query("INSERT INTO `$this->table_zt`(`contentid`, `headextend`, `banner`, `template`, `filepath`, `filename`) VALUES('$contentid', '$headextend', '$banner', '$template', '$filepath', '$filename')");
		}

		return $contentid;
	}

	function edit($data)
	{
		global $timestamp, $administrator;
		$contentid = $data['contentid'];
		$title = $data['title'];
		$stitle = $data['stitle'];
		$cid = $data['cid'];
		$origin = $data['origin'];
		$author = $data['author'];
		$editorid = $administrator['id'];
		$editor = $administrator['editor'];
		$content = SafeConvert($data['content']);
		$description = $data['description'] ? $data['description'] : cutstr(strip_tags($content), 50);
		$islink = $data['islink'];
		$url = $data['url'];
		$special = $data['special'];
		$status = $data['weighing'] && $data['weighing']>2 && $data['weighing']<99 ? $data['weighing'] : $data['status'];
		$showimg = $data['showimg'];
		if($showimg)
		{
			$this->db->query("UPDATE `$this->table` SET `showimg`='$showimg' WHERE `contentid`='$contentid'");
			@unlink(SITE_ROOT.'attachments'.$data['showimg_old']);
		}
		//修改数据
		$this->db->query("UPDATE `$this->table` SET cateid='$cid', title='$title', stitle='$stitle', description='$description', origin='$origin', author='$author', content='$content', islink='$islink', updatetime='$timestamp', status='$status' WHERE contentid='$contentid'");
		//更新URL
	
			if($islink)
			{
				$islink_info = $this->db->fetch_first("SELECT islink, url FROM `$this->table` WHERE `contentid`='$contentid'");
				if(!$islink_info['islink']) //如果以前非外链, 删除静态文件
				{
					unlink(SITE_ROOT.$islink_info['url']);
				}
				$this->db->query("UPDATE `$this->table` SET `url`='$url' WHERE `contentid`='$contentid'");
			}
			else
			{
				if($special == 1){
					if($data['filename'] != $data['oldfilename'] ){
						$oldurl = 'zt/'.$data['filepath'].'/'.$data['oldfilename'].'.shtml';
						@unlink(SITE_ROOT.$oldurl);
						$this->db->query("DELETE FROM `$this->table_zt` WHERE `filename`='$data[filename]'");
						$url = 'zt/'.$data['filepath'].'/'.$data['filename'].'.shtml';
						$this->db->query("UPDATE `$this->table` SET `url`='$url' WHERE `contentid`='$contentid'");
					}
				}else{
					$islink_old = $this->db->result_first("SELECT islink FROM `$this->table` WHERE `contentid`='$contentid'");
					if($islink_old == 1)
					{
						$url = 'a/'.date('y').'/'.date('m').'/'.$contentid.'.shtml';
						$this->db->query("UPDATE `$this->table` SET `islink`='$islink', `url`='$url' WHERE `contentid`='$contentid'");
					}
				}
			}
		//特殊主题
		if($special == 1 && !$islink) //专题
		{
			$headextend = $data['headextend'];
			$template = $data['template'];
			$filename = $data['filename'];
			$filepath = $data['filepath'];
			if($data['banner'])
			{
				$banner = $data['banner'];
				@unlink(SITE_ROOT.'attachments'.$data['banner_old']);
			}
			else
			{
				$banner = $data['banner_old'];
			}
			$this->db->query("REPLACE INTO `$this->table_zt`(`contentid`, `headextend`, `banner`, `template`, filepath, filename) VALUES('$contentid', '$headextend', '$banner', '$template', '$filepath', '$filename')");
		}

		return $contentid;
	}

	function del($contentid)
	{
		if(is_array($contentid))
		{
			$affarr=array_map(array(&$this, 'del'), $contentid);
			$aff=0;
			foreach($affarr as $k=>$v){
				$aff +=$v;
			}
		}
		else
		{
			$info = $this->db->fetch_first("SELECT cateid, islink, url, attachment, special FROM `$this->table` WHERE `contentid`='$contentid'");
			if(!$info['islink'])
			{
				@unlink(SITE_ROOT.$info['url']);
			}
			if($info['attachment'])
			{
				$query = $this->db->query("SELECT filepath FROM `{$this->table_attach}` WHERE contentid='$contentid'");
				while ($row = $this->db->fetch_array($query)) {
					@unlink(SITE_ROOT.'attachments'.$row['filepath']);
				}
				$this->db->query("DELETE FROM `{$this->table_attach}` WHERE contentid='$contentid'");
			}
			@unlink(SITE_ROOT.'attachments'.$info['thumbimg']);
			$this->db->query("DELETE FROM `$this->table` WHERE `contentid`='$contentid'");

			if($info['special'] == 1)
			{
				$bannerfile = $this->db->result_first("SELECT banner FROM `$this->table_zt` WHERE `contentid`='$contentid'");
				if($bannerfile) @unlink(SITE_ROOT.'attachments'.$bannerfile);
				$this->db->query("DELETE FROM `$this->table_zt` WHERE `contentid`='$contentid'");
			}
			$aff = $this->db->affected_rows();;
		}
		return $aff;
	}
}
?>